package com.neusoft.ruidaozaixian.controller.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.neusoft.ruidaozaixian.R;
import com.neusoft.ruidaozaixian.controller.activity.LoginActivity;
import com.neusoft.ruidaozaixian.controller.adapter.MyBaseExpandableListAdapter;
import com.neusoft.ruidaozaixian.model.bean.PostInformation;
import com.neusoft.ruidaozaixian.util.baseinfo.Connectinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class LearningPhaseFragment extends Fragment {
    private ArrayList<PostInformation.PostStageResListBean> postStageResList;
    private List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
    private List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
    private ExpandableListView expandList;
    private MyBaseExpandableListAdapter myAdapter;

    public LearningPhaseFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_learning_phase, container, false);

        expandList = (ExpandableListView) view.findViewById(R.id.expandlist);
        getDate();

        expandList.setGroupIndicator(null);//这里不显示系统默认的group indicator

        registerForContextMenu(expandList);//给ExpandListView添加上下文菜单
        //child子项的单击事件
        expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                String lpId = ((TextView)getActivity(). findViewById(R.id.lpId)).getText().toString();
                Toast.makeText(LearningPhaseFragment.this.getActivity(), "你单击了："
                        + lpId, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        return view;

    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
    public void getDate() {
        //创建一个Request
        FormBody formBody = new FormBody.Builder()
                .add("postid", PostCourseFragment.POST_ID)
                .build();
        final Request request = new Request.Builder()
                .addHeader("cookie", LoginActivity.s)
                .url(Connectinfo.obtainPositionClass)
                .post(formBody)
                .build();
        //new call
        Call call = LoginActivity.mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            //失败回掉
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //成功回掉
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.isSuccessful()) {

                    Message msg = Message.obtain();
                    msg.obj = response.body().string();
                    lpHandler.sendMessage(msg);

                }
            }

        });
    }

    private Handler lpHandler = new Handler() {
        public void handleMessage(Message msg) {
            String res = (String) msg.obj;
            Gson gson = new Gson();
            PostInformation postInformation=gson.fromJson(res, PostInformation.class);
            postStageResList=postInformation.getPostStageResList();
            Map<String, String> map;
            List<Map<String, String>> list;
            for (PostInformation.PostStageResListBean p:postStageResList){

                for (PostInformation.PostStageResListBean.ChildStageListBean c:p.getChildStageList()){
                    map = new HashMap<String, String>();
                    map.put("group", p.getParentStageName() + "--" + c.getStageName());
                    Map<String, String> map1;
                    list = new ArrayList<Map<String, String>>();
                    for (PostInformation.PostStageResListBean.ChildStageListBean.ChildStageResListBean s:c.getChildStageResList()){
                        map1 = new HashMap<String, String>();
                        map1.put("child",s.getResName());

                        map1.put("id", String.valueOf(s.getResId()));
                        list.add(map1);

                    }
                    childs.add(list);
                    groups.add(map);

                }


            }

            myAdapter = new MyBaseExpandableListAdapter(LearningPhaseFragment.this.getActivity(),groups,childs);
            expandList.setAdapter(myAdapter);



        }
    };
    /**
     * group和child子项的数据源
     */
    private void initEvents() {




        //在drawable文件夹下新建了indicator.xml，下面这个语句也可以实现group伸展收缩时的indicator变化
        //expandList.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));


    }




}
